<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="box">
      <span id="box1">1</span>
      <span id="box2">2</span>
      <span id="box3">3</span>
      <span id="box4">4</span>
      <span id="box5">5</span>
    </div>
  </body>
  <script>
    //第一种
    // box.onclick = function (event) {
    //     let target = event.target;
    //     switch (target.id) {
    //         case "box1":
    //             alert("1");
    //             break;
    //         case "box2":
    //             alert("2");
    //             break;
    //         case "box3": alert("3");
    //             break;
    //         case "box4":
    //             alert("4");
    //             break;
    //         default: alert("5");
    //     }
    // }
    //第二种
    document.addEventListener('click', function (event) {
      var target = event.target;
      switch (target.id) {
        case 'box1':
          alert('1');
          break;
        case 'box2':
          alert('2');
          break;
        case 'box3':
          alert('3');
          break;
        case 'box4':
          alert('4');
          break;
        default:
          alert('5');
      }
    });
  </script>
</html>
